package com.example.woke;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Datas_history;
import com.woke.method.Intentnet;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends Activity {

	private ListView mListView;
	private Myadapter adapter;
	private ArrayList<Datas_history> alllist = new ArrayList<Datas_history>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
		
		intview();
		
	}

	private void intview() {
		findViewById(R.id.history_image_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mListView = (ListView) findViewById(R.id.history_listview);
		adapter = new Myadapter();
		mListView.setAdapter(adapter);
		getdata();
	}
	
	private void getdata() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "public");
		params.put("a", "andriodList");
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("unknow")){
						
					}else if(flag.equals("success")){
						JSONObject object = response.getJSONObject("data");
						String string = object.getString("data");
						Gson gson=new Gson ();
						Type type=new TypeToken<List<Datas_history>> (){}.getType ();
//						ArrayList<Datas_history> datalist = (ArrayList<Datas_history>) JSONArray.parseArray(string, Datas_history.class);
						ArrayList<Datas_history> datalist=gson.fromJson (string,type);
						alllist.addAll(datalist);
						adapter.notifyDataSetChanged();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(HistoryActivity.this, "数据请求失败", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}

			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}

	class Myadapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return alllist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		class Holder {
			TextView mText_time;
			TextView mText_lili;
			TextView mText_name;
			TextView mText_money;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if(convertView == null){
				holder = new Holder();
				convertView = getLayoutInflater().inflate(R.layout.item_adter_history, null);
				holder.mText_time = (TextView) convertView.findViewById(R.id.item_history_time);
				holder.mText_name = (TextView) convertView.findViewById(R.id.item_history_content);
				convertView.setTag(holder);
			}else{
				 holder = (Holder) convertView.getTag();
			}
			
			Datas_history data = alllist.get(position);
			holder.mText_time.setText(Intentnet.getStrTime(data.getCreatetime()));
			holder.mText_name.setText(data.getContent());
			
			return convertView;
		}
		
	}
	
}
